

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.*;


public class RegexTest {

	  private class Rule  {
		    
		    public boolean sign;
		    public Pattern pattern;
		    public String s;
		    Rule(boolean sign, String regex) {
		      this.sign = sign;
		      this.pattern = Pattern.compile(regex);
		      this.s = regex;
		    }

		    protected boolean match() {
		      return pattern.matcher(this.s).find();
		    }
	  }
	  
	  
	 
	 public void test() throws IOException{
		 List<String> rules1 = new ArrayList<String>();
		 List<String> rules2 = new ArrayList<String>();
		 List<String> urls = new ArrayList<String>();
		 String rule1;
		 String rule2;
		 //String s = "http://news.chinaventure.com.cn/2/20120713/91570.shtml" ;
		 /*
		 Pattern p = Pattern.compile("^http://([0-9a-zA-Z]*.)*(sohu).com/");
		 Matcher m = p.matcher(s);
		 if ( m.find() )
		 System.out.println(m.group(0) + "  " );
		 */
		 //File file = new File("/Users/rmj/Documents/workspace/RegexTest/regex-urlfilter.txt");
		 File file1 = new File("regex-urlfilters.txt");
	     FileInputStream fis = new FileInputStream(file1);     
	     BufferedReader in1 = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
	     
	     File file2 = new File("regex-urlsamples.txt");
	     FileInputStream urs = new FileInputStream(file2);
	     BufferedReader in2 = new BufferedReader(new InputStreamReader(urs, "UTF-8")); 
	     
	    //File f = new File("urls.txt");
		//BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
		 try{
		        String line1;
		        String line2;
		        System.out.println(Pattern.matches("^http://([0-9a-zA-Z]+\\.)*appchina\\.com/([-0-9a-zA-Z_]+/)+", "http://de.appchina.com/reports/the-venture-capital-in-america-2012-q2-has-grown-to-8100-million/"));
		        while((line2=in2.readLine())!=null) {
		        	//System.out.println(line1);
		            
		        	
		        	Pattern p = Pattern.compile("(^http\\S*)");
		        	Matcher m = p.matcher(line2);
		        	if(m.find()) System.out.println(( m.group()));
		        	else continue;
		        	//if(parts2.length==1)parts2[0]=line2.trim();
			       
			        
		        	rule2  = m.group();
			          //rules2.add(rule2);
			          urls.add(rule2);
			          //out.append(rule2+"\n");
			        }
		        while((line1=in1.readLine())!=null) {
		        	//System.out.println(line1);
		            
		        	
			        
		        	Pattern p = Pattern.compile("(\\^http\\S*)");
		        	Matcher m = p.matcher(line1);
		        	if(m.find()) System.out.println((rule1  = m.group()));
		        	else continue;
			          
		        	rules1.add(rule1);
			          //rules2.add(rule2);
			    
			          //out.append(rule2+"\n");
			        }


		   
		    } catch (IOException e) {
		        e.printStackTrace();
		    }
		 int total = 0,error=0,correct=0;
		 for ( int i = 0; i < rules1.size() ; i++){
			 ++total;
			if ( Pattern.matches(rules1.get(i), urls.get(i)))
	        {
				//System.out.println("match!!! " + (i+1));
				++correct;
	        }
			else
			{
				System.out.println("not match!!!"+(i+1));
				++error;
			}
		 }		 
		 System.out.format("total:%d\ncorrect:%d\nerror:%d\n", total,correct,error);
	 }
	 
	 public static void main(String[] args) throws Exception{
		 System.out.println("Starts");
		 RegexTest rt = new RegexTest();
		 rt.test();
		 System.out.println("Ends");
	 }
}
